exports.api = function(ops) {
	var app = ops.app,
		cluster = ops.cluster,
		url = ops.url,
		s_config = ops.s_config,
		routes = ops.routes,
		grunt = require('grunt'),
		//RateLimit = ops.RateLimit,
		wss = ops.wss;

	var crossHeader = function(res){
		res.header('Access-Control-Allow-Origin', 'http://www.iliulan.com');
		res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
		res.header('Access-Control-Allow-Methods', 'POST, GET');
		res.header('Content-Security-Policy', "default-src 'self' *.iliulan.com");
	}

	var filter = function(ip, res) {
			/*var a = grunt.file.readJSON(path.resolve("user/ip.json"));
			ip = ip.split('.');
			//console.log(new RegExp(ip.join('\\.'), "gim"), new RegExp(ip.join('\\.'), "gim").test(a.data), a.data)
			if (new RegExp(ip.join('\\.'), "gim").test(a.data)) {
				console.log("filter: " + ip)
					//routes.err(res, '404');
				return false;
			}*/
			return true;
		},
		error = function(res, req, bool) {
			var query = url.parse(req.url, true).query;
			var b = query.callback;
			res.send(bool ? {
				status: 0,
				data: []
			} : b ? b + "(" + JSON.stringify({
				status: 0,
				data: []
			}) + ")" : {
				status: 0,
				data: []
			});
		};

	app.get("/", function(req, res) {
		console.log('worker /' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.index(s_config, res, query);
		} else {
			routes.err(res, '404');
		}
	});

	app.get("/fetchBaiduHotSearch", function(req, res) {
		console.log('worker /' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			routes.fetchBaiduHotSearch(res, req);
		} else {
			routes.err(res, '404');
		}
	});

	/*var intervalTasksLimiter = new RateLimit({
		windowMs: 3 * 1000, // 1 hour window
		delayAfter: 1, // begin slowing down responses after the first request
		delayMs: 1 * 500, // slow down subsequent responses by 3 seconds per request
		max: 5, // start blocking after 5 requests
		message: "Too many accounts created from this IP, Please try again after 30 minutes"
	});*/

	app.get(/\/intervalSTasks/, function(req, res) {
		console.log('worker /intervalSTasks' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			routes.intervalSTasks(res, req);
		} else {
			error(res, req);
		}
	});

	app.get(/\/intervalTasks/, function(req, res) {
		console.log('worker /intervalTasks' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.intervalTasks(res, query);
		} else {
			error(res, req);
		}
	});

	app.get(/\/intervalYoukuTasks/, function(req, res) {
		console.log('worker /intervalTasks' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.fetchVideo(res, query);
		} else {
			error(res, req);
		}
	});

	app.post(/\/channel/, function(req, res) {
		console.log('worker /channel - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.channel(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/videos/, function(req, res) {
		console.log('worker /videos - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.videos(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/videoinfo/, function(req, res) {
		console.log('worker /videoinfo - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.videoinfo(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/news/, function(req, res) {
		console.log('worker /news - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.news(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.get(/\/feed/, function(req, res) {
		console.log('worker /feed - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.feed(res, query);
		} else {
			error(res, req);
		}
	});

	app.get(/\/config/, function(req, res) {
		console.log('worker /config - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.config(res, query);
		} else {
			error(res, req);
		}
	});

	app.get(/\/getpopdata/, function(req, res) {
		console.log('worker /getpopdata - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.getpopdata(res, query, req);
		} else {
			error(res, req);
		}
	});

	app.post(/\/gettaobaopopdata/, function(req, res) {
		console.log('worker /gettaobaopopdata - ' + cluster.worker.id)
		console.log("PATH: " + req.url)

		crossHeader(res);
		/*var a = filter(req.ip, res);
		if (a) {*/
			var query = url.parse(req.url, true).query;
			routes.getTaobaoPopdata(res, query, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.get(/\/imgcaptcha/, function(req, res) {
		console.log('worker /imgcaptcha - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.imgcaptcha(res, query, req);
		} else {
			error(res, req);
		}
	});

	app.post(/\/submitinfo/, function(req, res) {
		console.log('worker /submitinfo' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			routes.submitinfo(res, req);
		} else {
			error(res, req, true);
		}
	});

	app.get(/\/getotherjson/, function(req, res) {
		console.log('worker /getotherjson' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.getotherjson(res, query);
		} else {
			error(res, req);
		}
	});

	app.get(/\/citynews/, function(req, res) {
		console.log('worker /citynews' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.citynews(res, query);
		} else {
			error(res, req);
		}
	});

	app.get(/\/countprops/, function(req, res) {
		console.log('worker /countprops' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {*/
			var query = url.parse(req.url, true).query;
			routes.countprops(res, query);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/getChannels/, function(req, res) {
		console.log('worker /getChannels' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.getChannels(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/search$/, function(req, res) {
		console.log('worker /search' + cluster.worker.id)
		console.log("PATH: " + req.url)

		crossHeader(res);
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.search(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/recommendkey/, function(req, res) {
		console.log('worker /recommendkey' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res);
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.recommendKey(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/recommendnews/, function(req, res) {
		console.log('worker /recommendnews' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res);
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.recommendNews(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/beforekeys/, function(req, res) {
		console.log('worker /beforekeys' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res);
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.beforeKeys(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/searchhotkeys/, function(req, res) {
		console.log('worker /searchhotkeys' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res);
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.searchhotkeys(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	/*app.get(/\/recommend/, function(req, res) {
		console.log('worker /recommend' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.recommend(res, query);
		} else {
			error(res, req);
		}
	});*/

	app.post(/\/addgood/, function(req, res) {
		console.log('worker /addgood' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.addgood(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/delgood/, function(req, res) {
		console.log('worker /delgood' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.delgood(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/forgotpassword/, function(req, res) {
		console.log('worker /forgotpassword' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {*/
			routes.forgotpassword(res, req);
		/*} else {
			error(res, req, true);
		}*/
	});

	app.post(/\/resetpassword/, function(req, res) {
		console.log('worker /resetpassword' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			routes.resetpassword(res, req);
		} else {
			error(res, req, true);
		}
	});

	app.post(/\/getuserinfo/, function(req, res) {
		console.log('worker /getuserinfo' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {*/
			//var query = url.parse(req.url, true).query;
			routes.getuserinfo(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/signin/, function(req, res) {
		console.log('worker /signin' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {*/
			routes.signin(res, req);
		/*} else {
			error(res, req, true);
		}*/
	});

	app.post(/\/signout/, function(req, res) {
		console.log('worker /signout' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {*/
			routes.signout(res, req);
		/*} else {
			error(res, req, true);
		}*/
	});

	app.post(/\/createaccount/, function(req, res) {
		console.log('worker /createaccount' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {*/
			routes.createaccount(res, req);
		/*} else {
			error(res, req, true);
		}*/
	});

	app.get(/\/getlinkedusers/, function(req, res) {
		console.log('worker /getlinkedusers' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.getlinkedusers(res, query);
		} else {
			error(res, req);
		}
	});

	app.post(/\/isfollow/, function(req, res) {
		console.log('worker /isfollow' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res);
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.isfollow(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/getusergoods/, function(req, res) {
		console.log('worker /getusergoods' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.getusergoods(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/getfollowusers/, function(req, res) {
		console.log('worker /getfollowusers' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.getfollowusers(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/getfollowerusers/, function(req, res) {
		console.log('worker /getfollowerusers' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.getfollowerusers(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/addfollow/, function(req, res) {
		console.log('worker /addfollow' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.addfollow(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/delfollow/, function(req, res) {
		console.log('worker /delfollow' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)

		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.delfollow(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.post(/\/usermessage/, function(req, res) {
		console.log('worker /usermessage' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.usermessage(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	/*wss && wss.on("connection", function(ws) {
		ws.on("message", function(msg) {
			console.log(msg)
			wss.clients.forEach(function(client) {
				client.send(msg);
			});
		})
	});
	app.ws("/push", function(ws, req) {
		console.log('worker /push' + cluster.worker.id)
		console.log("PATH: " + req.url)
		routes.push(ws, req);
	});*/

	app.post(/\/getfollowermessage/, function(req, res) {
		console.log('worker /getfollowermessage' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.getfollowermessage(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	var path = require('path');

	function addFilterIp(ip) {
		var a = grunt.file.readJSON(path.resolve("user/ip.json"));
		if (a && a.data) {
			a = a.data.split(',');
			a.push(ip);
			grunt.file.write(path.resolve("user/ip.json"), "{\"data\":\"" + a.join(',') + "\"}");
		}
	}


	app.post(/\/aip/, function(req, res) {
		console.log('worker /aip' + cluster.worker.id)
		console.log("PATH: " + req.url)

		crossHeader(res);
		//var a = filter(req.ip, res);
		//if (a) {
			routes.aip(res, req);
		//} else {
		//	error(res, req, true);
		//}
	});

	app.post(/\/fy/, function(req, res){
		console.log('worker /aip' + cluster.worker.id)
		console.log("PATH: " + req.url)

		crossHeader(res);

		routes.youdao(res, req);
	});


	app.get('*', function(req, res) {
		console.log('worker *' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var rurl = decodeURIComponent(req.url);
			var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？%+_]", "gim");
			rurl = rurl.replace(pattern, function(a) {
				return "\\" + a;
			});
			console.log(rurl)
			try {
				res.render(rurl, function(err, html) {
					if (err) {
						console.log("->", err);
						//console.log("addFilter: " + req.ip)
						//if (!/{{(pic|userico)+}}/.test(decodeURIComponent(req.url))) addFilterIp(req.ip);
						routes.err(res, '404');
					}
				});
			} catch (e) {
				console.log("->", e);
				routes.err(res, '404');
			}
		} else {
			routes.err(res, '404');
		}
	});

	//callback();
};